package com.jzo2o.customer.service.impl;

import com.jzo2o.api.publics.SmsCodeApi;
import com.jzo2o.common.enums.SmsBussinessTypeEnum;
import com.jzo2o.common.expcetions.BadRequestException;
import com.jzo2o.common.model.Result;
import com.jzo2o.common.utils.JwtTool;
import com.jzo2o.common.utils.StringUtils;
import com.jzo2o.customer.mapper.ServeProviderMapper;
import com.jzo2o.customer.model.domain.ServeProvider;
import com.jzo2o.customer.model.dto.request.LoginForWorkReqDTO;
import com.jzo2o.customer.service.IRegisterService;
import com.jzo2o.customer.service.IServeProviderService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class IRegisterServiceImpl implements IRegisterService {



    @Resource
    private JwtTool jwtTool;



    @Resource
    private IServeProviderService serveProviderService;

    @Resource
    private ServeProviderMapper serveProviderMapper;

    @Resource
    private SmsCodeApi smsCodeApi;
    @Override
    public Result<?> registerForWorker(LoginForWorkReqDTO loginForWorkReqDTO) {
        // 数据校验
        if (StringUtils.isEmpty(loginForWorkReqDTO.getVerifyCode())) {
            throw new BadRequestException("验证码错误，请重新获取");
        }
        //远程调用publics服务校验验证码是否正确


        boolean verifyResult = smsCodeApi.verify(loginForWorkReqDTO.getPhone(), SmsBussinessTypeEnum.INSTITUTION_RESET_PASSWORD, loginForWorkReqDTO.getVerifyCode()).getIsSuccess();


//        String redisKey = String.format(CommonRedisConstants.RedisKey.VERIFY_CODE, phone, bussinessType.getType());
//        String verifyCodeInRedis = redisTemplate.opsForValue().get(redisKey);
//
//        String redisKey = String.format(CommonRedisConstants.RedisKey.VERIFY_CODE, smsCodeSendReqDTO.getPhone(), smsCodeSendReqDTO.getBussinessType());

        System.out.println(verifyResult);
        if (!verifyResult) {
            throw new BadRequestException("验证码错误，请重新获取");
        }
        // 登录校验
        // 根据手机号和用户类型获取服务人员或机构信息
//        ServeProvider serveProvider = serveProviderService.findByPhoneAndType(loginForWorkReqDTO.getPhone(), loginForWorkReqDTO.getUserType());

        ServeProvider serveProvider = serveProviderService.findByPhone(loginForWorkReqDTO.getPhone());

        String pho = serveProvider.getPhone();

        String pa = loginForWorkReqDTO.getPassword();

        if (serveProvider != null) {
            // 修改密码
            serveProvider.setPassword(loginForWorkReqDTO.getPassword());
            serveProviderMapper.updatess(pa,pho);
        }

        return new Result(200, "密码修改成功", (Object)null);
    }

    @Override
    public String registerAsForWorker(LoginForWorkReqDTO loginForWorkReqDTO) {
        // 数据校验
        if (StringUtils.isEmpty(loginForWorkReqDTO.getVerifyCode())) {
            throw new BadRequestException("验证码错误，请重新获取");
        }
        //远程调用publics服务校验验证码是否正确


        boolean verifyResult = smsCodeApi.verify(loginForWorkReqDTO.getPhone(), SmsBussinessTypeEnum.INSTITION_REGISTER, loginForWorkReqDTO.getVerifyCode()).getIsSuccess();


//        String redisKey = String.format(CommonRedisConstants.RedisKey.VERIFY_CODE, phone, bussinessType.getType());
//        String verifyCodeInRedis = redisTemplate.opsForValue().get(redisKey);
//
//        String redisKey = String.format(CommonRedisConstants.RedisKey.VERIFY_CODE, smsCodeSendReqDTO.getPhone(), smsCodeSendReqDTO.getBussinessType());

        System.out.println(verifyResult);
        if (!verifyResult) {
            throw new BadRequestException("验证码错误，请重新获取");
        }
        // 登录校验
        // 根据手机号和用户类型获取服务人员或机构信息
//        ServeProvider serveProvider = serveProviderService.findByPhoneAndType(loginForWorkReqDTO.getPhone(), loginForWorkReqDTO.getUserType());

        ServeProvider serveProvider = serveProviderService.findByPhone(loginForWorkReqDTO.getPhone());

        if (serveProvider == null) {
            String pho = loginForWorkReqDTO.getPhone();

            int type = 3;

            int status = 0;
            String pa = loginForWorkReqDTO.getPassword();
            serveProviderMapper.inserta(pho,pa,type,status);
            return "注册成功";
        }

       return "该手机号已注册";
    }
}
